from selenium import webdriver
import time,unittest
from selenium.webdriver.common.by import By
from Day_2211A.Unit_09.mysql import Mysql
import HtmlTestRunner

class A3mall(unittest.TestCase):
    def setUp(self):
        self.sql = Mysql('10.59.9.12', 'root', 'yidong666', 'a3mall')

    def tearDown(self):
        self.sql.close()

    amount = None
    price = None
    freight = None
    bh = None

    def test_01(self):
        self.driver = webdriver.Edge()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.12:7001/')
        # 点击登录
        self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        # 定位账号输入框
        self.driver.find_element(By.NAME, 'username').send_keys('15523615757')
        # 定位密码输入框
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        time.sleep(8)
        # 定位登录按钮
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        time.sleep(5)
        # 文本断言，账号已登录
        dy = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print(dy)
        self.assertIn('15523615757', dy)
        # 账户余额
        amount = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        amount1 = amount.split(' ')
        A3mall.amount = amount1[1]
        print(A3mall.amount)
        # 定位搜索框
        self.driver.find_element(By.CSS_SELECTOR, '#searchForm > div.header-search > input').send_keys('小米')
        time.sleep(2)
        # 定位搜索按钮
        self.driver.find_element(By.CLASS_NAME, 'search-input-btn').click()
        time.sleep(3)
        # 选择第一个商品
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[1]/div[1]/span/a/img').click()
        time.sleep(3)
        # 断言商品价格是否与数据库价格一样
        dy2 = self.driver.find_element(By.ID, 'sell-price-box').text
        print(dy2)
        price = self.sql.chaxun('select sell_price from mall_goods where id = 26')
        A3mall.price = str(price[0][0])
        print(A3mall.price)
        self.assertEqual(dy2, A3mall.price)
        # 定位立即购买
        self.driver.find_element(By.ID, 'buy-cart').click()
        time.sleep(2)
        # 断言应付总额
        A3mall.freight = self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[4]/div[2]/div[2]/div/div/div[2]/span[2]').text
        print(A3mall.freight)
        # 定位下一步按钮
        self.driver.find_element(By.ID, 'order-button').click()
        time.sleep(5)
        dy4 = self.driver.find_element(By.CLASS_NAME, 'succe-ico').text
        print(dy4)
        self.assertIn("成功", dy4)
        # 定位查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        time.sleep(5)
        self.driver.close()
        self.driver.quit()
        # 定位申请退款
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[6]/td[2]/div[1]/a').click()
        time.sleep(2)
        #定位输入框，填写退款原因
        self.driver.find_element(By.CLASS_NAME, 'reason-text').send_keys('多买不想要了')
        time.sleep(2)
        # 定位提交按钮
        self.driver.find_element(By.ID, 'user-info-btn').click()
        time.sleep(6)
        # 提取订单编号
        A3mall.bh = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text


    def test_02(self):
        # 断言
        money = self.sql.chaxun('select amount from mall_users where username = "15523615757"')
        money1 = float(money[0][0])
        print(money1)
        self.assertEqual(float(A3mall.amount)-float(A3mall.freight), money1)

    def test_03(self):
        self.driver = webdriver.Edge()
        self.driver.maximize_window()
        # 打开A3Mall后台
        self.driver.get('http://10.59.9.12:7001/admin/login/index.html')
        # 定位输入框,输入账号
        self.driver.find_element(By.NAME, 'username').send_keys('admin')
        time.sleep(1)
        # 定位输入框,输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('admin888')
        time.sleep(12)
        # 点击登录
        self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[4]/button').click()
        time.sleep(2)
        # 断言超级管理员
        dy = self.driver.find_element(By.XPATH, '/html/body/div/aside/div/section/div/div[2]').text
        self.assertIn('超级管理员', dy)
        # 点击订单
        self.driver.find_element(By.XPATH, '/html/body/div/header/nav/div[1]/ul/li[4]/a').click()
        time.sleep(2)
        # 文本断言全部订单
        dy2 = self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/div/div[1]/div[1]/div[1]/div/div[1]').text
        self.assertEqual('全部订单', dy2)
        # 定位订单详情
        self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/div/div[2]/div[3]/div[2]/table/tbody/tr[1]/td/div/p[2]/a[1]').click()
        time.sleep(2)
        # 断言销售价格和购买时的商品价格是否相等
        jg = self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div/div[1]/div[1]/table/tbody/tr/td[3]').text
        self.assertEqual('42.00', jg)
        # 选择退款
        self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div/div[1]/div[1]/div/a[3]').click()
        time.sleep(2)
        # 定位选中
        self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div[1]/div/div[1]/table/tbody/tr/td[6]/div/span').click()
        time.sleep(2)
        # 断言订单编号和前台购买的商品编号是否一样
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div[1]/div/div[2]/table/tbody/tr[1]/td[1]').text
        self.assertEqual(A3mall.bh, dy3)
        # 定位说明输入框，输入退款原因
        self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div[1]/div/div[2]/table/tbody/tr[10]/td/textarea').send_keys('退款')
        time.sleep(2)
        # 点击立即提交
        self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div[2]/div/button[1]').click()
        time.sleep(2)
        # 断言作废
        dy4 = self.driver.find_element(By.XPATH, '/html/body/div/div/section/div/form/div/div/div[1]/div[1]/div/a[4]').text
        self.assertEqual('作废', dy4)

